<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\order;


use app\dao\order\StoreOrderStatusDao;
use app\services\BaseServices;
use crmeb\traits\ServicesTrait;
use think\exception\ValidateException;

/**
 * 订单状态
 * Class StoreOrderStatusServices
 * @package app\services\order
 * @mixin StoreOrderStatusDao
 */
class StoreOrderStatusServices extends BaseServices
{
    use ServicesTrait;

    /**
     * 构造方法
     * StoreOrderStatusServices constructor.
     * @param StoreOrderStatusDao $dao
     */
    public function __construct(StoreOrderStatusDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 订单状态分页
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getStatusList(array $where)
    {
        $list = $this->dao->getStatusList($where);
        foreach ($list as &$item) {
            if (is_int($item['change_time'])) $item['change_time'] = date('Y-m-d H:i:s', $item['change_time']);
        }
        $count = $this->dao->count($where);
        return compact('list', 'count');
    }

	/**
	 * 保存订单记录
	 * @param string $type
	 * @param int $orderId
	 * @param array $data
	 * @return bool
	 */
	public function saveStatus(string $type, int $orderId, array $data = [])
	{
		if (!$type || !$orderId) {
			return true;
		}
		$statusData = ['oid' => $orderId, 'change_time' => time(), 'change_type' => ''];
		switch ($type) {
			case 'create'://创建
				$statusData['change_type'] = 'cache_key_create_order';
				$statusData['change_message'] = '订单生成';
				if (isset($data['changePrice']) && $data['changePrice'] > 0) {//有改价
					$this->saveStatus('edit', $orderId, $data);
				}
				break;
			case 'edit'://改价
				$statusData['change_type'] = 'order_edit';
				$statusData['change_message'] = '商品总价为：' . ($data['totalPrice'] ?? 0) . ' 修改实际支付金额为：' . ($data['payPrice'] ?? 0);
				break;
			case 'pay'://支付
				$statusData['change_type'] = 'pay_success';
				$statusData['change_message'] = '用户付款成功';
				break;
			case 'delivery'://发货
                $statusData['change_type'] = 'delivery';
                $statusData['change_message'] = '发货成功';
                break;
			case 'take'://收货
                if (!$this->dao->count(['oid' => $orderId, 'change_type' => ['take_delivery', 'user_take_delivery']])) {//是否已经写入收货消息
                    $statusData['change_type'] = 'take_delivery';
                    $statusData['change_message'] = '已收货';
                }
				break;
			case 'reply'://评价
				break;
			default:
				throw new ValidateException('未知订单类型记录');
				break;
		}
        $this->dao->save($statusData);
		return true;
	}

}
